<?php
/**
 * 用户业务逻辑类
 * @author zengnz
 *
 */
class UserLogic extends CommonModel
{
	// 用户基本信息
	const USER_BASE = 'USER_BASE_INFO_';
	
	const OPEN_USER_LIST = 'OPEN_USER_LIST';
	
	// 缓存模型类
	private $_CacheModel = '';
	
	private $_Model = '';
	
	public function __construct() {
		$this -> _CacheModel = D('Cache', 'Service');
		$this -> _CacheModel -> init();
		$this -> _Model = new Model();
	}
	
	public function getTipInfo() {
		$hour = Date('H');
		$tips = UserModel::$tips;
		return $tips[$hour];
	}
	
	//获取时间区域
	public function getTimeArea() {
		$hour = date('H');
	
		$timeArea = '';
		if ($hour >= 3 && $hour < 6)
			$timeArea = '凌晨';
		else if ($hour >= 6 && $hour < 8)
			$timeArea = '早晨';
		else if ($hour >= 8 && $hour < 11)
			$timeArea = '上午';
		else if ($hour >= 11 && $hour < 13)
			$timeArea = '中午';
		else if ($hour >= 13 && $hour < 17)
			$timeArea = '下午';
		else if ($hour >= 17 && $hour < 19)
			$timeArea = '傍晚';
		else if ($hour >= 19 && $hour < 23)
			$timeArea = '晚上';
		else
			$timeArea = '午夜';
	
		return $timeArea;
	}
	
	/**
	 * 获取用户基本信息
	 * @param integer $uId
	 */
	public function getUserBaseInfo($uId) {
		$key = self::USER_BASE . $uId;
		$this -> _CacheModel -> rm($key);
		$baseInfo = $this -> _CacheModel -> get($key);
		
		if (!$baseInfo) {
			$baseInfo = $this -> _Model -> table('customer')
						-> field('num,account,email,vip_lv,create_time,last_login_time,last_login_ip,login_times,user_sign,status')
						-> where('id='.$uId)
						-> find();
			
			$this -> _CacheModel -> set($key, $baseInfo);
			$this -> _CacheModel -> saveKeys($key, 'user', $uId);
		}
		
		return $baseInfo;
	}
	
	/**
	 * 新用户注册
	 */
	public function doRegister() {
		$Model = D('User');
		$ret = $Model -> create();
		
		if (!$ret) {
			$this -> error = $Model -> getError();
			return 0;
		}	
		
		$Model -> num = createUserAccount();
		$Model -> pwd = buildPassword($Model->account, $Model->pwd);
		$Model -> create_time = time();
		$Model -> status = 1;
		$Model -> vip_lv = 1;
		
		$ret = $Model -> add();
		if (!$ret)
			$this -> error = '用户注册失败！';
			
		return $Model -> getLastInsID();
	}
	
	/**
	 * 初始化用户信息
	 * @param integer $uId 用户ID
	 * @param string $account 用户名
	 */
	public function initUserInfo($uId, $account) {
		$inteInit = D('Integration', 'Logic') -> initIntegration($uId, $account);
		if (!$inteInit) {
			$this -> error = '初始化积分信息失败！';
			return 0;
		}
		
		$initFund = D('Funds', 'Logic') -> initFunds($uId, $account);
		if (!$initFund) {
			$this -> error = '初始化资金信息失败！';
			return 0;
		}
	
		return 1;
	}
	
	
	/**
	 * 获取开放交友的用户
	 */
	public function getOpenUser() {
		$key = self::OPEN_USER_LIST;

		$userList = $this -> _CacheModel -> get($key);
		if (!$userList) {
			$Model = D('User');
			$userList = $Model -> field('id, num, account')
						-> where('open=1') 
						-> select();
			
			$this -> _CacheModel -> set($key, $userList);
			$this -> _CacheModel -> saveKeys($key, 'common');
		}
		
		return $userList;
	}
	
	
	
}